package com.example.demo.security.jwt;

import com.example.demo.model.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class JWTUtils {

    private final String header="Authorization";
    private final String secret="lstznkj2020";
    private long expiration=1000 * 24 * 60 * 60 * 7;;

    private Logger logger= LoggerFactory.getLogger(JWTUtils.class);

    public String getHeader() {
        return header;
    }

    public String getSecret() {
        return secret;
    }

    public long getExpiration() {
        return expiration;
    }


//    public static final String TOKEN_HEADER = "Authorization";
//    public static final String TOKEN_PREFIX = "";
//
//    public static final String SUBJECT = "my";
//
//    public static final long EXPIRITION = 1000 * 24 * 60 * 60 * 7;
//
//    public static final String APPSECRET_KEY = "my_secret";

//    private static final String ROLE_CLAIMS = "role";



    public  String generateJsonWebToken(User user) {

        if ((user.getUsername() == null) || (user.getPassword() == null)) {
            return null;
        }

        Map<String,Object> map = new HashMap<>();
        map.put("role", "role");

        String token = Jwts
                .builder()
                .setSubject(user.getUsername())
                .setClaims(map)
                .claim("username", user.getUsername())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + expiration))
                .signWith(SignatureAlgorithm.HS512, secret).compact();
        return token;
    }

    /**
     * 生成token
     * @param username
     * @param role
     * @return
     */
    public  String createToken(String username,String role) {

        logger.info("start to create token");


        String token = Jwts
                .builder()
                .setSubject(username)
                .claim("username",username)
                .claim("role",role)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + expiration))
                .signWith(SignatureAlgorithm.HS512, secret).compact();
        return token;
    }

    public Claims checkJWT(String token) {
        try {
            final Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
            return claims;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取用户名
     * @param token
     * @return
     */
    public  String getUsername(String token){
        Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        return claims.get("username").toString();
    }

    /**
     * 获取用户角色
     * @param token
     * @return
     */
    public  String getUserRole(String token){
        Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        return claims.get("role").toString();
    }

    /**
     * 是否过期
     * @param token
     * @return
     */
    public  boolean isExpiration(String token){
        Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        return claims.getExpiration().before(new Date());
    }


}